//
//  CollectionViewController.m
//  freeLance
//
//  Created by Igor Karpenko on 3/27/13.
//  Copyright (c) 2013 Mozi Development. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "CustomLayout.h"

#import "CollectionViewController.h"


@interface CollectionViewController ()

@property (nonatomic, strong) CDProducts *selectedProduct;

@end

@implementation CollectionViewController


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"CollectionViewControllerToProductViewController"]) {
        ProductViewController *desinationViewController = (ProductViewController *)[segue destinationViewController];
        desinationViewController.product = self.selectedProduct;
    }
}


#pragma mark - Interface methods

-(void) viewDidLoad {
    [super viewDidLoad];
    
    self.title = _collection.name;
	_dataSource = [[CollectionDataSource alloc] initWithCollection:_collection];
//	_point = point;
    
    CGRect newFrame = [UIScreen mainScreen].bounds;
    newFrame.origin.y -= 64;
    _coverView = [[UIView alloc] initWithFrame:newFrame];
    self.coverView.backgroundColor = [UIColor colorWithWhite:0.3f alpha:0.2f];
    self.coverView.alpha = 0.0f;
    
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(dismissProductViewController:)];
    [self.coverView addGestureRecognizer:tapRecognizer];
    
    [self.view addSubview:_coverView];
	
	UINib *cellNib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
	[_collectionView registerNib:cellNib forCellWithReuseIdentifier:collectionCellIdentifier];
	
	UINib *headerNib = [UINib nibWithNibName:@"CollectionHeaderView" bundle:nil];
	[_collectionView registerNib:headerNib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
			 withReuseIdentifier:collectionHeaderIdentifier];

	if (IS_IPAD) {
		CustomLayout *layout = [[CustomLayout alloc] init];
		layout.stackCenter = CGPointMake(_point.x + 112.0f, _point.y + 178.0f);
		_collectionView.collectionViewLayout = layout;
	}
	_collectionView.dataSource = _dataSource;
}


-(void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	
	if (IS_IPAD) {
		UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back"
																		   style:UIBarButtonItemStyleBordered
																		  target:self
																		  action:@selector(backButtonTapped)];

		UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
										   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
										   target:nil action:nil];
		negativeSpacer.width = 30;
		self.navigationItem.leftBarButtonItems = [NSArray
												  arrayWithObjects:negativeSpacer, backButtonItem, nil];
	}
}


-(void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	
	if (IS_IPAD) {
		[_collectionView performBatchUpdates:^{
			((CustomLayout *)_collectionView.collectionViewLayout).stackFactor = 1.0;
		} completion:nil];
	}
}


#pragma mark - Action methods

-(void) backButtonTapped {
	if (IS_IPAD) {
		[self.navigationController dismissViewControllerAnimated:YES completion:nil];
	} else {
		[self.navigationController popViewControllerAnimated:YES];
	}

}


-(void) dismissProductViewController:(UIGestureRecognizer *)recognizer {
    for (UIViewController *controller in self.childViewControllers) {
        [UIView animateWithDuration:0.2f animations:^{
            controller.view.transform = CGAffineTransformMakeScale(0.3f, 0.3f);
            controller.view.alpha = 0.0f;
            self.coverView.alpha = 0.0f;
        } completion:^(BOOL finished) {
            [controller.view removeFromSuperview];
            [controller removeFromParentViewController];
        }];
    }
}

#pragma mark - Delegated methods

-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedProduct = _dataSource.items[indexPath.row];

    [self performSegueWithIdentifier:@"CollectionViewControllerToProductViewController" sender:self];    
}


-(void)productViewControllerDidColose:(ProductViewController *)sender {
    [UIView animateWithDuration:0.2f animations:^{
        self.coverView.alpha = 0.0f;
    }];
}

@end
